using System;
using System.Collections.Generic;
using Castle.ActiveRecord;
using Castle.Components.Validator;
using NHibernate.Criterion;

namespace DecisionMaking.Domain
{
	[ActiveRecord]
	public class AlgorithmDefinition : ActiveRecordValidationBase<AlgorithmDefinition>
	{
		public AlgorithmDefinition ()
		{
		}
		
	    [PrimaryKey(PrimaryKeyType.Native)]
	    public int Id {set; get;}

        [BelongsTo(NotNull = true)]
        public Account Account {set; get;}
		
		[Property(UniqueKey = "uix_algorithm_definition_name"), ValidateLength(2, 50)]
		public string Name {set; get;}

		[Property(NotNull = true)] 
		public string Engine {set; get;}
		
		[Property(ColumnType = "String", Length = 10000)]
		public string Code {set; get;}		

        /// <summary>
        /// Returns all algorithm definitions by the specified account(s)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static IList<AlgorithmDefinition> FindByAccountIds(ICollection<int> ids)
        {
            DetachedCriteria criteria = DetachedCriteria.For(typeof (AlgorithmDefinition));
            criteria.CreateCriteria("Account").Add(Restrictions.InG("Id", ids));

            return FindAll(criteria);
        }
		
		public override bool Equals (object obj)
		{
			if (obj == null)
				return false;
			if (ReferenceEquals (this, obj))
				return true;
			if (obj.GetType () != typeof(AlgorithmDefinition))
				return false;
			AlgorithmDefinition other = (AlgorithmDefinition)obj;
			return Id == other.Id;
		}


		public override int GetHashCode ()
		{
			unchecked {
				return Id.GetHashCode ();
			}
		}

	}
}

